//
//  AXFProductsCell.swift
//  SpuerYangAngWang
//
//  Created by 杨茂盛 on 2016/12/23.
//  Copyright © 2016年 杨茂盛. All rights reserved.
//

import UIKit
import SDWebImage
class AXFProductsCell: UITableViewCell {
    var model :ZHSellFastModel?{
        didSet{
            //在此处赋值
            self.name.text = model?.name
            self.icon .sd_setImage(with: URL.init(string: (model?.img!)!))
            self.standard.text = model?.specifics
            self.price.text = model?.partner_price
            self.market_price.text = model?.market_price
            
            //判断是否热卖
            if model?.had_pm == "1"{
            self.zengsong.isHidden = false
            }
        }
    }
    //头像
    @IBOutlet weak var icon: UIImageView!
    //标题
    @IBOutlet weak var name: UILabel!
    //规格
    @IBOutlet weak var standard: UILabel!
    //打折后的价格
    @IBOutlet weak var price: UILabel!
    //市场价格
    @IBOutlet weak var market_price: UILabel!
    //是否热卖
    @IBOutlet weak var zengsong: UIImageView!
    
    //加号减号 view
   lazy var clickView  = AXFMarketClickView.ClickGoodsView()
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.selectionStyle = .none
        self.contentView.addSubview(clickView)
        clickView.snp.makeConstraints { (make) in
            make.trailing.equalTo(self.contentView).offset(-5)
            make.centerY.equalTo(self.market_price)
         
            make.width.equalTo(60)
            make.height.equalTo(30)
        }
        clickView.addTarget(self, action: #selector(clickBtn), for: .valueChanged)
    }
    
    //点击当前 view 的监听方法
    func clickBtn(){
        model?.count = clickView.idex
        if clickView.isLeftOrRight {
            //获取按钮的中心点
            let btnCenter = self.icon.center
            //获取 keywindow
            let keyWindow = UIApplication.shared.keyWindow
            //把按钮的中心点化为window上的坐标
            
            /*
             转换的原因：
             
             为什么要转换?
             有抛物线动画, 起点 , 控制器, 终点, 他们的坐标系需要在 同一个坐标系中, 相对于shopViewController的坐标
             
             */

            //开始转换
            let converCenter = self.convert(btnCenter, to: keyWindow)
            //发出通知,传递转换好的坐标和模型
         NotificationCenter.default.post(name: NSNotification.Name(rawValue: AddBtnNotification), object: self.model, userInfo: ["point":converCenter,"goodsImageView":self.icon!])
        }else{
        //点击减少
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: ReduceBtnNotification), object: self.model)
        }
    }
    
}
